<template>
  <div class="layout">
    <div class="header">
      <jc-button
        :btn-arr="['reload', 'save', 'remove', 'pass', 'sub', 'option', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'com1', 'com2', 'copy', 'freeze']"
        :list="'/supplierProcessList'"
        @save="handleAdd"
        @refresh="otherDetail"
        @submit="handleAdd(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @forbidden="using(1)"
        @startUsing="using(2)"
        @del="delectHandle"
      />
    </div>
    <el-tabs v-model="essential" @tab-click="handleClick">
      <el-tab-pane :label="$t('market.info')" name="information">
        <el-form ref="purchaseRef" :model="process" label-width="100px">
          <el-form-item :label="$t('market.state')">
            <el-tag :type="process.stateColor">{{ process.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="process.fbillno" size="mini" :disabled="isDisabled" />
          </el-form-item>
          <el-form-item :label="$t('market.supplier')">
            <el-autocomplete
              v-model.trim="process.fsupplier"
              highlight-first-item
              popper-class="my-autocomplete"
              :fetch-suggestions="querySearch9"
              size="mini"
              :disabled="isDisabled"
              @focus="handleSupplier"
              @select="supplierData"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="supplierVisiblit=true"
              />
              <template slot-scope="{ item }">
                <div class="name">{{ item.fname }}</div>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-form>
        <div>
          <div class="btnHeader">
            <el-button size="mini" :disabled="isDisabled" @click="twoLine(1)">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" :disabled="isDisabled" @click="twoLine(2)">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" :disabled="isDisabled" @click.native="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
            <search
              :options="selectData"
              :search-son="searchSon"
              :msg="tacitly"
              @seek="collect"
              @hand="otherDetail1"
            />
            <el-button type="primary" class="btn" size="mini" @click="otherDetail1">
              {{ $t('btn.search') }}
            </el-button>
          </div>
          <jc-table
            :table-data="tableData"
            :table-header="tableHeader"
            :table-height="tableHeight"
            serial
            :get-summaries="getSummaries"
            show-summary
            @rowClick="rowClick"
            @cellClick="cellClick"
          >
            <!--    工序名称       -->
            <template v-slot:billSlot="col">
              <el-input v-model="col.scope.row.fworkname" size="mini" :disabled="isDisabled">
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="sourcing(col.scope.$index)"
                />
              </el-input>
            </template>
            <!--    倍数       -->
            <template v-slot:btnState="col">
              <el-input-number
                v-model="col.scope.row.fnum"
                size="mini"
                :disabled="isDisabled"
                @change="headelMoney(col.scope.$index)"
              />
            </template>
            <!--    单价       -->
            <template v-slot:btnStates="col">
              <el-input-number
                v-model="col.scope.row.fprice"
                size="mini"
                :disabled="isDisabled"
                @change="headelMoney(col.scope.$index)"
              />
            </template>
            <!--    备注       -->
            <template v-slot:orderSlot="col">
              <el-input
                v-model="col.scope.row.fremark"
                size="mini"
                :disabled="isDisabled"
              />
            </template>
            <template v-slot:btnSlot="col">
              <el-button size="mini" type="danger" :disabled="isDisabled" @click="delEntry(col.scope.row)">{{ $t('btn.del') }}</el-button>
            </template>
          </jc-table>
        </div>
        <!--    分页器-->
        <div class="footer">
          <jc-pagination
            :total="total"
            :page.sync="pageNum"
            :limit.sync="pageSize"
            @pagination="otherDetail1"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="process.fbillNo"
          other-height="65vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--供应商-->
    <supplier v-if="supplierVisiblit" @supplier="supplierData" />
    <outSourcing v-if="sourcingVis" @supplier="sourcData" />
  </div>
</template>

<script>
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import jcMarker from '@/components/marker'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'
import {
  deleteWorkSupplier,
  queryWorkSupplierHeader, updateWorkSupplier,
  workSupplierAudit, workSupplierDisable,
  workSupplierSubmit,
  workSupplierUnAudit, workSupplierUnDisable,
  workSupplierUnSubmit,
  queryWorkSupplierDetails, deleteWorkSupplierDetails
} from '@/api/workshop'
import jumpMateriel from '@/components/JumpMateriel'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'

export default {
  name: 'OtherWarehousingAdd',
  components: {
    search,
    supplier: () => import('@/views/purchasing/purchase/treePurchasePrice/component/supplier'),
    outSourcing: () => import('@/views/workshop/supplierProcess/supplierProcessAdd/component/outSourcing'),
    jcMarker,
    jcButton
  },
  mixins: [autocomplete, jumpMateriel, searData],
  data() {
    return {
      ftype: 77,
      total: 0, // 总条目
      pageNum: 1, // 当前页
      pageSize: 50, // 每页显示多少条数据
      essential: 'information',
      supplierVisiblit: false, // 供应商
      sourcingVis: false, // 工序
      fnumber: '',
      otherUrlObject: {},
      // 表头数据
      tableData: [],
      tableHeader: [
        { label: this.$t('market.processname'), prop: 'fnumber', minWidth: '100' },
        { label: this.$t('market.processnumber'), type: 'bill', prop: 'fworkname', minWidth: '100' },
        { label: this.$t('market.multiple'), type: 'state', prop: 'fnum', minWidth: '80' },
        { label: this.$t('market.fprice'), type: 'states', prop: 'fprice', minWidth: '80' },
        { label: this.$t('market.billamount'), prop: 'fsumprice', minWidth: '80' },
        { label: this.$t('market.note'), type: 'order', prop: 'fremark', minWidth: '80' },
        { label: this.$t('market.handle'), type: 'btn', width: '120' }
      ],
      isDisabled: false,
      process: {},
      tableHeight: '',
      delArr: [],
      indexSelf: 0 // 表格index
    }
  },
  created() {
    this.otherDetail()
    this.otherDetail1()
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 270px)' : '400px'
  },
  methods: {
    // 详情
    otherDetail() {
      const fid = this.$route.params.id
      queryWorkSupplierHeader({ fid }).then(res => {
        if (res.code === 0) {
          this.process = toMxBmina(res.data)
          this.otherUrlObject = res.data
          this.isDisabled = this.process.fdocumentstatus === this.$t('market.state1') || this.process.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 详情
    otherDetail1() {
      const fid = this.$route.params.id
      queryWorkSupplierDetails({
        search: this.searCollData,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        fid
      }).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.detail
          this.total = res.data.total
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 修改
    handleAdd(ev) {
      const data = {
        ...notoMxBmina(this.process),
        detail: this.tableData
      }
      updateWorkSupplier(data).then(res => {
        const { code, message } = res
        if (code !== 0) {
          this.$confirm(message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        if (ev === 1) {
          this.submit(1)
        } else {
          this.otherDetail()
          this.otherDetail1()
        }
        this.$message({ showClose: true, message: res.message, type: 'success' })
      })
    },
    // 删除
    delEntry(ev) {
      this.fentryid = ev.fentryid
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        deleteWorkSupplierDetails({ fentryid: this.fentryid }).then(res => {
          if (res.code === 0) {
            this.pageNum = 1
            this.otherDetail1()
            this.$message({ showClose: true, message: res.message, type: 'success' })
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 获取供应商
    supplierData(ev) {
      if (ev.fsupplierId) {
        this.process.fsupplierid = ev.fsupplierId
        this.process.fsupplier = ev.fname
      }
      this.supplierVisiblit = false
    },
    // 工序名称
    sourcing(index) {
      this.sourcingVis = true
      this.indexSelf = index
    },
    sourcData(ev) {
      if (ev.fid) {
        this.tableData[this.indexSelf].fworkname = ev.fname
        this.tableData[this.indexSelf].fnumber = ev.fnumber
        this.tableData[this.indexSelf].fworknameid = ev.fid
      }
      this.sourcingVis = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.process.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.otherDetail()
        this.otherDetail1()()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? workSupplierSubmit : workSupplierUnSubmit
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? workSupplierAudit : workSupplierUnAudit
      this.toExamineFun(api)
    },
    // 禁用
    async using(ev) {
      const api = ev === 1 ? workSupplierDisable : workSupplierUnDisable
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const DATA = { Numbers: [this.process.fbillno] }
        deleteWorkSupplier(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/supplierProcessList' })
          this.$message({ showClose: true, message: res.message, type: 'success' })
        })
      })
    },
    // 获取金额
    headelMoney(index) {
      const item = this.tableData[index]
      item.fsumprice = item.fnum * item.fprice
    },
    // 点击tabs
    handleClick(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 新增 删除行
    twoLine(ev) {
      const data = {
        fnumber: '',
        fworkname: '',
        fnum: 1,
        fprice: 0,
        fsumprice: 0,
        fremark: '',
        fentryid: 0
      }
      if (ev === 1) {
        this.tableData.push(data)
      } else {
        const item = this.tableData[this.indexSelf]
        this.delEntry(item)
      }
    },
    // 点击单元格
    cellClick(row, column) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      const ckid = this.tableData[this.subscript].fstockId
      const sumQty = this.tableData[this.subscript].stockQty
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          if (this.property === 'fstockName') {
            res.fstockId = ckid
            res.stockQty = sumQty
          }
          res[this.property] = item
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.layout {
  @include listBom;
  padding: 10px;
  background: #FFFFFF;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-input__icon {
    line-height: 30px;
  }
  .btnHeader {
    position: relative;
    margin-bottom: 10px;
  }
  .search {
    margin-left: 300px;
    padding-top: 0;
  }
  .btn {
    margin-left: 520px !important;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      //height: 35px;
      width: 275px;
      margin-bottom: 0;
      .btnType{
        margin-right: 20px;
      }
      .el-date-editor.el-input{
        width: 100% !important;
      }
      .el-select{
        width: 100%;
      }
    }
  }
}
</style>
